package model

import (
	"gorm.io/gorm"
	"smart-health-platforms/config"
	"time"
)

type BargainActivity struct {
	Id        uint32         `gorm:"column:id;type:int UNSIGNED;primaryKey;not null;" json:"id"`
	BargainId int32          `gorm:"column:bargain_id;type:int;default:NULL;" json:"bargain_id"`
	UserId    int32          `gorm:"column:user_id;type:int;default:NULL;" json:"user_id"`
	SkrId     int32          `gorm:"column:skr_id;type:int;default:NULL;" json:"skr_id"`
	People    int32          `gorm:"column:people;type:int;default:0;" json:"people"`
	Price     float64        `gorm:"column:price;type:decimal(10, 2);default:NULL;" json:"price"`
	Status    int32          `gorm:"column:status;type:int;comment:1-砍价进行中2-砍价结束，待支付3-已支付4-订单已完成;default:NULL;" json:"status"` // 1-砍价进行中2-砍价结束，待支付3-已支付4-订单已完成
	Code      string         `gorm:"column:code;type:varchar(255);default:NULL;" json:"code"`
	OrderSn   string         `gorm:"column:order_sn;type:varchar(255);default:NULL;" json:"order_sn"`
	StrTime   time.Time      `gorm:"column:str_time;type:datetime(3);comment:砍价开始时间;default:CURRENT_TIMESTAMP(3);" json:"str_time"` // 砍价开始时间
	EndTime   time.Time      `gorm:"column:end_time;type:datetime(3);comment:砍价结束时间;default:CURRENT_TIMESTAMP(3);" json:"end_time"` // 砍价结束时间
	CreatedAt time.Time      `gorm:"column:created_at;type:datetime(3);default:CURRENT_TIMESTAMP(3);" json:"created_at"`
	UpdatedAt time.Time      `gorm:"column:updated_at;type:datetime(3);default:CURRENT_TIMESTAMP(3);" json:"updated_at"`
	DeletedAt gorm.DeletedAt `gorm:"column:deleted_at;type:datetime(3);default:NULL;" json:"deleted_at"`
}

func (b *BargainActivity) TableName() string {
	return "bargain_activity"
}
func (b *BargainActivity) AddBargainActivity() error {
	return config.DB.Create(&b).Error
}
func (b *BargainActivity) UpdateBargainActivity() error {
	return config.DB.Model(*b).Updates(&b).Error
}
func (b *BargainActivity) DeletedBargainActivity() error {
	return config.DB.Model(&b).Delete(&b).Error
}
func (b *BargainActivity) GetCode() error {
	return config.DB.Where("code = ?", b.Code).Limit(1).Find(&b).Error
}
func (b *BargainActivity) GetBargainActivity() error {
	return config.DB.Where("id = ?", b.Id).First(&b).Error
}
func (b *BargainActivity) GetOrderSn() error {
	return config.DB.Where("order_sn = ?", b.OrderSn).Limit(1).Find(&b).Error
}
func (b *BargainActivity) BargainList() (ba []BargainActivity, err error) {
	err = config.DB.Where("user_id = ?", b.UserId).Find(&ba).Error
	return
}
